package cn.jimu98.controller;

import cn.jimu98.common.lang.Result;
import cn.jimu98.entity.User;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Classname AuthController
 * @Description TODO
 * @Date 2020/6/10 18:19
 * @Created by kaibo
 */
@Controller
public class AuthController extends BaseController{

    private static final String KAPTCHA_SESSION_KEY="KAPTCHA_SESSION_KEY";
    @Autowired
    Producer producer;

    @GetMapping("/capthca.jpg")
    public void Kaptcha(HttpServletResponse resp) throws IOException {
        //验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        req.getSession().setAttribute(KAPTCHA_SESSION_KEY,text);

        resp.setHeader("Cache-Control","no-store, no-cache");
        resp.setContentType("image/jpeg");
        ServletOutputStream outputStream = resp.getOutputStream();
        ImageIO.write(image,"jpg",outputStream);

    }
    @ResponseBody
    @PostMapping("/register")
    public Result doRegister(User user,String repass,String vercode){
        String capthca = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        System.out.println(capthca);
        return Result.success().action("/login");
    }
    @GetMapping("/login")
    public String login(){
        return "/auth/login";
    }
    @GetMapping("/register")
    public String register(){
        return "/auth/reg";
    }
}
